
from xadmin import site as adminx_site
from .forms import *
from django.shortcuts import render_to_response
# Register your models here.


def repo_init_store_unit_action(modeladmin, request, queryset):
    form = None
    return render_to_response('admin/repo/init_store_unit.html')
repo_init_store_unit_action.short_description = '初始化仓库存储'


class RepoAdmin(object):
    form = RepoForm
    list_display = ('id', 'name', 'no', 'price_per_100g_1d')

    def get_readonly_fields(self, request, obj=None):
        if obj is None:
            return []
        else:
            return ['no', ]

    def save_model(self, request, obj, form, change):
        cdata = form.cleaned_data
        obj.save()
adminx_site.register(Repo, RepoAdmin)


class StoreUnitAdmin(object):
    list_display = ['id', 'no', 'repo', 'fields', 'in_use']
    search_fields = ['id', 'repo_no']
    fields = ['repo', 'parent_type', 'repo_no', 'in_use']

    def get_readonly_fields(self, request, obj=None):
        return [f.name for f in  self.model._meta.fields]

    def has_add_permission(self, request):
        return False
    def has_delete_permission(self, request, obj=None):
        return False
adminx_site.register(StoreUnit, StoreUnitAdmin)
